<template>
  <div class="internship-introduction-container">
    <a-card title="实习介绍生成" :bordered="false">
      <a-alert
        message="上传您的简历，系统将为您生成详细的实习经历介绍"
        description="良好的实习经历介绍能够凸显您的实践能力、专业技能和职业发展潜力"
        type="info"
        show-icon
        style="margin-bottom: 20px"
      />

      <div class="upload-section">
        <a-upload
          v-model:file-list="fileList"
          :before-upload="beforeUpload"
          :remove="handleRemove"
          :max-count="1"
          :multiple="false"
          :accept="'.pdf,.doc,.docx'"
        >
          <a-button type="primary" :disabled="fileList.length >= 1">
            <upload-outlined /> 上传简历
          </a-button>
          <div class="upload-hint">支持 PDF、Word 格式文件</div>
        </a-upload>
      </div>

      <a-divider />

      <div class="action-buttons">
        <a-button
          type="primary"
          :disabled="fileList.length === 0 || loading"
          @click="generateInternships"
          :loading="loading"
        >
          生成实习介绍
        </a-button>
        <a-button
          :disabled="!internshipContent"
          @click="copyInternshipContent"
          style="margin-left: 10px"
        >
          复制内容
        </a-button>
      </div>

      <a-divider />

      <div class="result-section">
        <a-spin :spinning="loading">
          <div v-if="internshipContent" class="result-content">
            <a-collapse v-model:activeKey="activeKey">
              <a-collapse-panel key="1" header="实习经历">
                <a-typography-paragraph>{{ internshipContent }}</a-typography-paragraph>
              </a-collapse-panel>
            </a-collapse>
          </div>
          <a-empty v-else description="暂无内容，请上传简历并点击生成" />
        </a-spin>
      </div>
    </a-card>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { message } from 'ant-design-vue'
import { UploadOutlined } from '@ant-design/icons-vue'
// @ts-ignore: 忽略模块类型声明错误
import { doSpecialExerciseUsingPost } from '@/api/specialExerciseController'

// 文件列表
const fileList = ref<any[]>([])
// 实习介绍内容
const internshipContent = ref<string>('')
// 加载状态
const loading = ref<boolean>(false)
// 展开的面板
const activeKey = ref<string[]>(['1'])

// 文件上传前检查
const beforeUpload = (file: File) => {
  // 检查文件类型
  const isValidFileType =
    file.type === 'application/pdf' ||
    file.type === 'application/msword' ||
    file.type === 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'

  if (!isValidFileType) {
    message.error('只能上传 PDF 或 Word 文件!')
    return false
  }

  // 检查文件大小
  const isLessThan10M = file.size / 1024 / 1024 < 10
  if (!isLessThan10M) {
    message.error('文件必须小于 10MB!')
    return false
  }

  // 文件验证通过，但返回false阻止自动上传
  return false
}

// 删除文件
const handleRemove = () => {
  fileList.value = []
  internshipContent.value = '' // 清空结果
  return true
}

// 生成实习介绍
const generateInternships = async () => {
  if (fileList.value.length === 0) {
    message.warning('请先上传简历')
    return
  }

  loading.value = true
  message.success('请稍后查看实习介绍')

  try {
    // 获取上传的文件对象
    const file = fileList.value[0].originFileObj
    // 调用后端接口生成实习介绍
    const params = { type: 'internship_introduction' }
    const data = await doSpecialExerciseUsingPost(params, {}, file)

    if (data.code === 0) {
      console.log('接口返回数据:', data.data)

      // 直接将返回的数据作为字符串显示
      if (typeof data.data === 'string') {
        internshipContent.value = data.data
      } else if (data.data && typeof data.data === 'object') {
        // 如果返回的是对象，转为JSON字符串
        internshipContent.value = JSON.stringify(data.data)
      } else {
        internshipContent.value = '返回数据格式不符合预期'
      }

      message.success('实习介绍生成成功')
    } else {
      message.error(data.message || '生成失败，请重试')
    }
  } catch (error) {
    console.error('生成失败:', error)
    message.error('生成失败，请重试')
  } finally {
    loading.value = false
  }
}

// 复制实习介绍内容
const copyInternshipContent = () => {
  if (!internshipContent.value) {
    message.warning('暂无内容可复制')
    return
  }

  navigator.clipboard
    .writeText(internshipContent.value)
    .then(() => {
      message.success('已复制到剪贴板')
    })
    .catch((err) => {
      console.error('复制失败:', err)
      message.error('复制失败，请手动复制')
    })
}
</script>

<style scoped>
.internship-introduction-container {
  padding: 20px;
}

.upload-section {
  margin: 20px 0;
}

.upload-hint {
  color: #999;
  margin-top: 8px;
  font-size: 12px;
}

.action-buttons {
  display: flex;
  justify-content: center;
  margin: 20px 0;
}

.result-section {
  margin-top: 20px;
}

:deep(.ant-upload-list-item-name) {
  font-weight: bold;
}

:deep(.ant-collapse-header-text) {
  font-weight: 600;
  color: #1890ff;
}
</style> 